<template>
	<view class="family-container">
		<view class="top">
			<view class="h2">{{ userStore.familyName }}</view>
		</view>
		<view class="body">
			<view class="item" v-for="(i, index) in userStore.members" :key="index">
				{{ i }}
			</view>
		</view>
		<view class="function">
			<view class="row">
				<input type="text" v-model="joinFamilyData" />
				<view class="txt" @click="joinFamly">
					加入家庭
				</view>
			</view>
			<view class="row">
				<view class="readonly-input" @click="copyFamilyId">
					<text>{{ familyId }}</text>
				</view>
				<text class="copy-tip txt">点击复制</text>
				<view class="txt" style="background-color: #ccc;">
					家庭链接码
				</view>
			</view>
			<view class="row">
				<p style="color:#ccc">通过他人的家庭链接码进入其他家庭，其他人也可以通过你的家庭链接码加入你的家庭</p>
			</view>
			<view class="row">
				<view class="txt" style="margin: 0 auto;" @click="leaveFamily">
					退出家庭
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { useUserStore } from '../../store/User';
import {
	ref,
	onBeforeMount,
	computed,
	watch
} from 'vue';

const userStore = useUserStore();
const joinFamilyData = ref('');
const joinFlag = ref(false); // 用于防抖

// 使用 ref 存储当前的 userInfo
const currentUserInfo = ref(uni.getStorageSync('userInfo'));

// 监听本地存储变化的函数
const updateUserInfo = () => {
	currentUserInfo.value = uni.getStorageSync('userInfo');
};

// 使用计算属性，但基于 currentUserInfo
const userId = computed(() => currentUserInfo.value._id);
const username = computed(() => currentUserInfo.value.username);
const familyId = computed(() => currentUserInfo.value.familyId);

// methods
const joinFamly = () => {
	// 判断是否为重复加入
	if (joinFamilyData.value === familyId.value) {
		uni.showToast({
			title: '您已加入此家庭',
			icon: 'none'
		});
		return;
	}
	// 判断是否为空
	if (!joinFamilyData.value) {
		uni.showToast({
			title: '请输入家庭链接码',
			icon: 'none'
		});
		return;
	}
	joinFlag.value = true;
	if (joinFlag.value) {
		// 调用云函数
		uniCloud.callFunction({
			name: 'joinFamily',
			data: {
				oldFamilyId: familyId.value,
				userId: userId.value,
				userName: username.value,
				familyId: joinFamilyData.value
			},
			success: res => {
				if (res.result.code === 404) {
					uni.showToast({
						title: '家庭不存在',
						icon: 'none'
					});
					return;
				}
				// 让页面刷新更新本地存储
				let info = uni.getStorageSync('userInfo');
				info.familyId = res.result.data.familyId;
				uni.removeStorageSync('userInfo');
				uni.setStorageSync('userInfo', info);

				// 更新当前页面的数据
				updateUserInfo();

				// 设置刷新标记
				userStore.setFlashFlag(true);

				setTimeout(() => {
					uni.switchTab({
						url: '/pages/index/index',
						success: () => {
							uni.showToast({
								title: res.result.msg,
								icon: 'success'
							});
						}
					});
				}, 100);
			},
			complete: () => {
				joinFlag.value = false;
			}
		});
	}
};

const leaveFamily = () => {
	// 首先判断家庭内是否只有一个人
	if (userStore.members.length === 1) {
		uni.showToast({
			title: '您已是此家庭的唯一成员，无法退出',
			icon: 'none'
		});
		return;
	}
	// 获取原本的家庭_id,以及用户信息
	uniCloud.callFunction({
		name: 'leaveFamily',
		data: {
			oldFamilyId: familyId.value,
			username: username.value
		}
	}).then(res => {
		// 让页面刷新更新本地存储
		let info = uni.getStorageSync('userInfo');
		info.familyId = res.result.data.familyId;
		uni.removeStorageSync('userInfo');
		uni.setStorageSync('userInfo', info);

		// 更新当前页面的数据
		updateUserInfo();

		// 设置刷新标记
		userStore.setFlashFlag(true);

		setTimeout(() => {
			uni.switchTab({
				url: '/pages/index/index',
				success: () => {
					uni.showToast({
						title: res.result.msg,
						icon: 'success'
					});
				}
			});
		}, 100);
	});
};

// 添加复制功能
const copyFamilyId = () => {
	if (!familyId.value) {
		uni.showToast({
			title: '暂无家庭链接码',
			icon: 'none'
		});
		return;
	}

	uni.setClipboardData({
		data: familyId.value,
		success: () => {
			uni.showToast({
				title: '已复制到剪贴板',
				icon: 'success'
			});
		}
	});
};

// 监听 store 中的数据变化
watch(() => userStore.familyName, () => {
	updateUserInfo();
});

// 页面加载时初始化数据
onBeforeMount(() => {
	updateUserInfo();
});
</script>

<style scoped lang="scss">
@import "../../uni.scss";

.family-container {
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 40rpx;
	background-color: $base-color1;

	.top {
		width: 100%;
		flex: 1;

		.h2 {
			box-sizing: border-box;
			width: 100%;
			margin-top: 20px;
			padding: 40px;
			font-size: 30px;
			text-align: center;
			background-color: $base-color2;
			color: $title-color;
		}
	}

	.body {
		width: 100%;
		flex: 3;
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		grid-column-gap: 40rpx;
		grid-row-gap: 0px;
		padding: 40rpx;
		box-sizing: border-box;
		background-color: $base-color2;

		.item {
			width: 200rpx;
			height: 200rpx;
			line-height: 200rpx;
			text-align: center;
			background-color: $btn-primary-color;
			border-radius: 50%;
			animation: dynatic 2s infinite;
			font-size: 40rpx;
			font-weight: 700;
			color: #000;
			box-shadow: 0 0 10rpx 4rpx rgba(0, 0, 0, 0.5);
		}
	}

	.function {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		gap: 40rpx;
		background-color: $base-color2;
		padding-top: 10px;

		.row {
			width: 80%;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			input,
			.input {
				flex-shrink: 0;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 40rpx;
				border: 1rpx solid #000;
				padding: 0 20rpx;
				box-sizing: border-box;
			}

			.txt {
				width: 300rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				background-color: $btn-primary-color;
				border-radius: 40rpx;
				margin-left: 10rpx;

				&:active {
					background-color: $btn-danger-color;
				}
			}
		}
	}
}

@keyframes dynatic {
	0% {
		transform: translateY(-16rpx);
	}

	50% {
		transform: translateY(16rpx);
	}

	100% {
		transform: translateY(-16rpx);
	}
}

.readonly-input {
	flex-shrink: 0;
	height: 80rpx;
	line-height: 80rpx;
	border-radius: 40rpx;
	border: 1rpx solid #000;
	padding: 0 20rpx;
	box-sizing: border-box;
	background-color: #f5f5f5;
	color: #666;
	position: relative;

	// 添加点击效果
	&:active {
		opacity: 0.8;
	}

	.copy-tip {
		position: absolute;
		right: 20rpx;
		font-size: 24rpx;
		color: #007aff;
	}
}

input[readonly] {
	pointer-events: none;
	background-color: #f5f5f5;
	opacity: 0.8;
	-webkit-user-select: none;
	user-select: none;
}
</style>